﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4ExportTroubles : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4ExportTroubles()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4ExportTroubles.DoubleClick += F4LocalTroubles_DoubleClick;
            this.F4ExportTroubles.PopupMenuShowing += F4LocalTroubles_PopupMenuShowing;
            this.f4F4ExportTroublesContextMenu.ItemClicked += f4LocalTroublesContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = F4ExportTroubles.GetFocusedDataRow();
            frm_Edit_F4ExportTroubles editForm = new frm_Edit_F4ExportTroubles();
            editForm.id = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                editForm.Dispose();
                MessageBox.Show("Cannot edit selected record.");
            }
        }

        void f4LocalTroublesContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4F4ExportTroublesContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4ExportTroubles add_Form = new frm_Add_F4ExportTroubles();
                    add_Form.ShowDialog();
                    LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4LocalTroubles_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4F4ExportTroublesContextMenu.Show(MousePosition);
        }

        private void F4LocalTroubles_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT t1.ID, t1.[Year], t1.[CustomerID], t1.[Date], t1.[ReportDate],"
                 + " t1.[CheckNo], t1.[ReportNo], t1.[NGTime], t1.[Month], t1.TimePlanNo AS OrderNo, t1.SerialNo, t2.CompID,"
                 + " ISNULL(t6.PartEnglishName, t2.CompID) AS CompName, t1.Kind, t1.[ExportQuantity], t1.[NGQuantity],"
                 + " CAST((ROUND((t1.[NGQuantity]/t1.[ExportQuantity]), 4) * 100) AS VARCHAR(MAX)) + '%' AS NGPercent,"
                 + " t1.F4TroubleContent, t1.[NGCode], t4.StaffName AS MadeBy, t3.StaffName AS Checker,"
                 + " t1.[TroubleKind], t1.Reason, t1.[ReasonCode], t1.Solution, t1.NGType, t1.SupplierID, t1.NGKind,"
                 + " t5.StaffName AS ResponsePerson, t1.Remark, t1.AssNote"
                 + " FROM [231_F4Troubles] AS t1 LEFT OUTER JOIN"
                 + " [231_TimePlan] AS t2 ON t2.TimePlanNo=t1.TimePlanNo LEFT OUTER JOIN"
                 + " [222_Staff] AS t3 ON t3.StaffID=t1.Checker LEFT OUTER JOIN"
                 + " [222_Staff] AS t4 ON t4.StaffID=t1.[MadeBy] LEFT OUTER JOIN"
                 + " [222_Staff] AS t5 ON t5.StaffID=t1.[ResponsePerson] LEFT OUTER JOIN"
                 + " [222_Part] AS t6 ON t2.CompID=t6.PartNo"
                 + " WHERE t1.Type=1"
                 + " ORDER BY t1.[Date]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4ExportTroubles.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(string));
                newTable.Columns.Add("Year", typeof(string));
                newTable.Columns.Add("CustomerID", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("ReportDate", typeof(DateTime));
                newTable.Columns.Add("CheckNo", typeof(string));
                newTable.Columns.Add("ReportNo", typeof(string));
                newTable.Columns.Add("NGTime", typeof(int));
                newTable.Columns.Add("Month", typeof(string));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("CompName", typeof(string));
                newTable.Columns.Add("Kind", typeof(string));
                newTable.Columns.Add("ExportQuantity", typeof(float));
                newTable.Columns.Add("NGQuantity", typeof(float));
                newTable.Columns.Add("NGPercent", typeof(string));
                newTable.Columns.Add("F4TroubleContent", typeof(string));
                newTable.Columns.Add("NGCode", typeof(string));
                newTable.Columns.Add("MadeBy", typeof(string));
                newTable.Columns.Add("Checker", typeof(string));                               
                newTable.Columns.Add("TroubleKind", typeof(string));
                newTable.Columns.Add("Reason", typeof(string));
                newTable.Columns.Add("ReasonCode", typeof(string));
                newTable.Columns.Add("Solution", typeof(string));
                newTable.Columns.Add("NGType", typeof(string));
                newTable.Columns.Add("SupplierID", typeof(string));
                newTable.Columns.Add("NGKind", typeof(string));
                newTable.Columns.Add("ResponsePerson", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                newTable.Columns.Add("AssNote", typeof(string));
                gridF4ExportTroubles.DataSource = newTable;
            }
            F4ExportTroubles.Columns[0].Caption = "ID";
            F4ExportTroubles.Columns[1].Caption = "Year";
            F4ExportTroubles.Columns[2].Caption = "Customer";
            F4ExportTroubles.Columns[3].Caption = "Real Export Date";
            F4ExportTroubles.Columns[4].Caption = "Report Date";
            F4ExportTroubles.Columns[5].Caption = "Customer Report No.";
            F4ExportTroubles.Columns[6].Caption = "Ass. Dept. Report No.";
            F4ExportTroubles.Columns[7].Caption = "Lần xảy ra lỗi";
            F4ExportTroubles.Columns[8].Caption = "Month";
            F4ExportTroubles.Columns[9].Caption = "Order No.";
            F4ExportTroubles.Columns[10].Caption = "Serial No.";
            F4ExportTroubles.Columns[11].Caption = "Model No.";
            F4ExportTroubles.Columns[12].Caption = "Model Name";
            F4ExportTroubles.Columns[13].Caption = "Kind (New/Old)";
            F4ExportTroubles.Columns[14].Caption = "Export Q'ty";
            F4ExportTroubles.Columns[15].Caption = "NG Q'ty";
            F4ExportTroubles.Columns[16].Caption = "% NG";
            F4ExportTroubles.Columns[17].Caption = "Mistake name";
            F4ExportTroubles.Columns[18].Caption = "Mistake code";
            F4ExportTroubles.Columns[19].Caption = "Made by";
            F4ExportTroubles.Columns[20].Caption = "Check By";
            F4ExportTroubles.Columns[21].Caption = "Mistake Type";
            F4ExportTroubles.Columns[22].Caption = "Reason";
            F4ExportTroubles.Columns[23].Caption = "Reason code";
            F4ExportTroubles.Columns[24].Caption = "Sollution";
            F4ExportTroubles.Columns[25].Caption = "Loại lỗi";
            F4ExportTroubles.Columns[26].Caption = "Nhà cung cấp";
            F4ExportTroubles.Columns[27].Caption = "Kiểu lỗi";
            F4ExportTroubles.Columns[28].Caption = "Person in charge";
            F4ExportTroubles.Columns[29].Caption = "Remark";
            F4ExportTroubles.Columns[30].Caption = "Evaluation";
            F4ExportTroubles.Columns[0].Visible = false;
            F4ExportTroubles.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {            
            frm_Add_F4ExportTroubles add_Form = new frm_Add_F4ExportTroubles();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }
    }
}
